package com.linx.web.init;

import com.alibaba.fastjson.JSON;
import com.linx.common.core.domain.model.DictEnumBody;
import com.linx.common.core.enums.BaseDictEnum;
import com.linx.system.domain.SysDictData;
import com.linx.system.domain.SysDictType;
import com.linx.system.domain.bo.SysDictDataBo;
import com.linx.system.domain.bo.SysDictTypeBo;
import com.linx.system.mapper.SysDictDataMapper;
import com.linx.system.mapper.SysDictTypeMapper;
import com.linx.system.service.ISysDictDataService;
import com.linx.system.service.ISysDictTypeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 初始化字典
 */
@Slf4j
@Component
public class InitDict implements ApplicationRunner {

    @Autowired
    private SysDictTypeMapper dictTypeMapper;
    @Autowired
    private ISysDictTypeService insertDictType;
    @Autowired
    private SysDictDataMapper dictDataMapper;
    @Autowired
    private ISysDictDataService dictDataService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void run(ApplicationArguments args) {
        List<SysDictType> dictTypeList = dictTypeMapper.selectList(null);
        List<SysDictData> dictDataList = dictDataMapper.selectList(null);

        for (DictEnumBody dictEnum: BaseDictEnum.list()){
            SysDictType sysDictType = dictTypeList.stream().filter(dt -> dictEnum.getCode().equalsIgnoreCase(dt.getDictType())).findAny().orElse(null);

            //  添加字典类型
            if (sysDictType == null) {
                sysDictType = new SysDictType();
                sysDictType.setDictType(dictEnum.getCode());//  编号
                sysDictType.setDictName(dictEnum.getLabel()); //  名称
                sysDictType.setDataFrom(1);    //  来源（1：系统默认；2：用户添加；）

                insertDictType.insertDictType(JSON.parseObject(JSON.toJSONString(sysDictType), SysDictTypeBo.class));
            }

            //  添加字典内容
            for (DictEnumBody subDictEnum: dictEnum.getSubDictList()) {
                String dictType = sysDictType.getDictType();
                SysDictData sysDictData = dictDataList.stream().filter(
                    dd -> dd.getDictType().equalsIgnoreCase(dictType) && subDictEnum.getCode().equalsIgnoreCase(dd.getDictValue())
                ).findAny().orElse(null);

                if (sysDictData == null) {
                    SysDictData subDict = new SysDictData();
                    subDict.setDictType(sysDictType.getDictType());
                    subDict.setDictSort(subDictEnum.getSort()); // 排序
                    subDict.setDictValue(subDictEnum.getCode()); // 编号
                    subDict.setDictLabel(subDictEnum.getLabel()); // 名称

                    dictDataService.insertDictData(JSON.parseObject(JSON.toJSONString(subDict), SysDictDataBo.class));
                }
            }
        }

        log.info("----------- 初始化字典 -----------");
    }

}
